package paramcalibration

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type Person struct {

	// 限制Age属性不能为空并且大于10
	Age int `form:"age" binding:"required,gt=10"`
	// 限制Name属性不能为空
	Name string `form:"name" binding:"required"`
	// TIPS：golang的time.Format方法一定要用2006-01-02 15:04:05作参数
	Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`
}

// StructCal 结构体验证。用gin框架的数据验证，可以不用解析数据，减少if else，会简洁许多
func StructCal(ctx *gin.Context) {

	person := Person{}
	if err := ctx.ShouldBind(&person); err != nil {
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, gin.H{"data": person})
}
